package com.zhuiyun.project.api.learnPlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zhuiyun.project.api.learnPlan.entity.LearnContent;
import com.zhuiyun.project.api.learnPlan.entity.LearnPlan;
import com.zhuiyun.project.api.learnPlan.mapper.LearnPlanContentMapper;
import com.zhuiyun.project.api.learnPlan.mapper.LearnPlanMapper;
import com.zhuiyun.project.api.learnPlan.model.LearnPlanModel;
import com.zhuiyun.project.api.learnPlan.service.LearnPlanService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName LearnPlanService

 * @Description TODO
 * @Author Z
 * @Date 2023/12/6 15:41
 **/
@Service
public class
LearnPlanServiceImpl implements LearnPlanService {

    @Autowired
    private LearnPlanMapper learnPlanMapper;

    @Autowired
    private LearnPlanContentMapper learnPlanContentMapper;

    /**
     * @Author z
     * @Description 计划查询
     * @Date 2023/12/19 11:43
     * @Return
     */
    @Override
    public CommonResult selectall(LearnPlan learnPlan) {
        //查询所有计划
        List<LearnPlan> learnPlans =  learnPlanMapper.selectAll(learnPlan);
        for (LearnPlan list:learnPlans) {
            //获取计划id
           Integer planId = list.getPlanId();
           //根据计划id查询学习内容
           List<LearnContent> contents = learnPlanContentMapper.selectByPlanId(planId);
           list.setLearnPlanContent(contents);
        }

        return CommonResult.ok(learnPlans);
    }

    /**
     * @Author zf
     * @Date 2024/2/26 16:04 TODO 添加学习计划内容小程序
     * @return CommonResult
     */
    @Override
    public CommonResult insertPlanQ(LearnPlanModel learnPlanModel) throws CommonException {
        LearnPlan learnPlan= BeanUtil.toBean(learnPlanModel, LearnPlan.class);
        learnPlan.setStatus(1);
        int i=learnPlanMapper.insertLearnPlanQ(learnPlan);
        if(i>0){
            LearnContent learnContent=BeanUtil.toBean(learnPlanModel, LearnContent.class);
            learnContent.setPlanId(learnPlan.getPlanId());
            int t=learnPlanContentMapper.insertLearnPlanContent(learnContent);
            return CommonResult.ok(EmErrorCode.SAVE_OK);
        }else {
            return CommonResult.error(EmErrorCode.SAVE_ERROR);
        }
    }
}
